package com.spring.extjs.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.extjs.dao.IMenuGroupDAO;
import com.spring.extjs.dao.IModuleDAO;
import com.spring.extjs.model.MenuGroup;
import com.spring.extjs.model.Module;
import com.spring.extjs.pageModel.ApplicationInfo;
import com.spring.extjs.pageModel.ServiceInfo;
import com.spring.extjs.pageModel.SystemInfo;
import com.spring.extjs.pageModel.UserInfo;
import com.spring.extjs.service.IApplicationService;

@Service
public class ApplicationServiceImpl implements IApplicationService {

	@Autowired
	private IMenuGroupDAO menuGroupDAO;
	@Autowired
	private IModuleDAO moduleDAO;
	
	private static List<Module> modules = null; 

	@Override
	public ApplicationInfo getApplicationInfo() {

		ApplicationInfo result = new ApplicationInfo();

		// 以上内容暂时为自定义的，以后会改为从数据库和登录信息中读取。
		SystemInfo systemInfo = new SystemInfo();
		systemInfo.setTf_systemName("自定义的中小型管理系统");
		systemInfo.setTf_systemVersion("2014.09.28");
		result.setSystemInfo(systemInfo);

		UserInfo userInfo = new UserInfo();
		userInfo.setTf_userdwmc("重庆上地房地产股份有限公司");
		userInfo.setTf_userStartdate(new Date());
		userInfo.setTf_userName("管理员");
		userInfo.setTf_loginName("admin");
		userInfo.setTf_userId(0);
		userInfo.setTf_departmentId("00");
		userInfo.setTf_departmentName("工程部");
		result.setUserInfo(userInfo);

		ServiceInfo serviceInfo = new ServiceInfo();
		serviceInfo.setTf_serviceDepartment("熙旺公司");
		serviceInfo.setTf_serviceMen("陈杰");
		serviceInfo.setTf_serviceTelnumber("18584566507");
		serviceInfo.setTf_serviceFaxnumber("0510-88888888");
		serviceInfo.setTf_serviceQQ("929483857");
		serviceInfo.setTf_serviceEmail("chj_0507_dz@sina.com");
		serviceInfo.setTf_serviceHomepage("");
		serviceInfo.setTf_copyrightInfo("熙旺公司版权所有");
		serviceInfo.setTf_copyrightOwner("熙旺软件");

		result.setServiceInfo(serviceInfo);

		// 把所有的模块定义信息加进去
		result.setTf_Modules(new HashSet<Module>(moduleDAO.selectList()));

		// 加入菜单分组
		result.setTf_MenuGroups(menuGroupDAO.selectList());
		
		for (MenuGroup mg : result.getTf_MenuGroups()) {  
            // 加入这一条是为了让菜单组下面的菜单也执行sql 语句加进来，不然的话，返回以后mvc要加入菜单，  
            // 就会在执行sql的时候因为session已经关闭而报错  
            mg.getTf_menuModules().size();  
        }  
  
        modules = new ArrayList<Module>(result.getTf_Modules());  

		return result;
	}
	
	// 根据模块 name 号取得模块定义  
    public static Module getModuleWithName(String name) {  
        for (Module module : getModules())  
            if (module.getTf_moduleName().equals(name))  
                return module;  
        return null;  
    }

	public static List<Module> getModules() {
		return modules;
	}

	public static void setModules(List<Module> modules) {
		ApplicationServiceImpl.modules = modules;
	}
}
